package com.zhong.framework.web.service;

import com.zhong.common.core.domain.entity.SysMenu;
import com.zhong.common.core.domain.entity.SysRole;
import com.zhong.common.core.domain.entity.SysUser;
import com.zhong.common.utils.StringUtils;
import com.zhong.system.domain.SysRoleMenu;
import com.zhong.system.domain.SysUserRole;
import com.zhong.system.service.SysMenuService;
import com.zhong.system.service.SysRoleMenuService;
import com.zhong.system.service.SysRoleService;
import com.zhong.system.service.SysUserRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 用户权限处理
 *
 * @author zhongzc
 */
@Component
@RequiredArgsConstructor
public class SysPermissionService
{
    private final SysRoleService roleService;

    private final SysMenuService menuService;

    private final SysUserRoleService userRoleService;

    private final SysRoleMenuService roleMenuService;

    /**
     * 获取角色数据权限
     *
     * @param user 用户信息
     * @return 角色权限信息
     */
    public Set<String> getRolePermission(SysUser user)
    {
        Set<String> roles = new HashSet<String>();
        // 管理员拥有所有权限
        if (user.isAdmin())
        {
            roles.add("admin");
        }
        else
        {
            roles.addAll(roleService.selectRolePermissionByUserId(user.getId()));
        }
        return roles;
    }

    /**
     * 获取菜单数据权限
     *
     * @param user 用户信息
     * @return 菜单权限信息
     */
    public Set<String> getMenuPermission(SysUser user)
    {
        Set<String> perms = new HashSet<String>();
        // 管理员拥有所有权限
        if (user.isAdmin())
        {
            perms.add("*:*:*");
        }
        else
        {
            List<SysUserRole> urList = userRoleService.findListByUserId(user.getId());
            if(!urList.isEmpty()){
                List<SysRole> roleList = roleService.findListByIdList(urList.stream().map(SysUserRole::getRoleId).collect(Collectors.toList()));
                if(!roleList.isEmpty()){
                    List<SysRoleMenu> rmList = roleMenuService.findListByRoleIdList(roleList.stream().map(SysRole::getId).collect(Collectors.toList()));
                    if(!rmList.isEmpty()){
                        List<SysMenu> menuList = menuService.findListByIdList(rmList.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList()));
                        for(SysMenu menu : menuList){
                            if (StringUtils.isNotEmpty(menu.getPerms())){
                                for(String perm : Arrays.asList(menu.getPerms().trim().split(","))){
                                    if (!perms.contains(perm)){
                                        perms.add(perm);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return perms;
    }
}
